package com.chenyun.system.feign;

import cn.hutool.core.bean.BeanUtil;
import com.chenyun.system.model.SysRegion;
import com.chenyun.system.service.ISysRegionService;
import com.chenyun.system.vo.SysRegionVO;
import com.chenyun.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * SysRegionFeign：
 * Description:
 *
 * @author: Lonni
 * @date: 2022/11/8 0008 15:17
 */
@RestController
@Slf4j
public class SysRegionFeign implements ISysRegionFeign,Serializable {

    @Resource
    @Lazy
    private ISysRegionService sysRegionService;

    @Override
    public List<SysRegionVO> getProvinces() {
        List<SysRegion> provinces = sysRegionService.getProvinces();
        return  this.convertEntityToVo(provinces);

    }

    @Override
    public List<SysRegionVO> getCitys(String proId) {
        if (StringUtils.isBlank(proId)) {
            log.debug("proId 为空,返回空数组 ");
            return  new ArrayList<>();
        }
        List<SysRegion> provinces = sysRegionService.getCityByProId(proId);
        return  this.convertEntityToVo(provinces);
    }

    @Override
    public List<SysRegionVO> getAreas(String cid) {
        if (StringUtils.isBlank(cid)) {
            log.debug("cid 为空,返回空数组 ");
            return  new ArrayList<>();
        }
        List<SysRegion> provinces = sysRegionService.getAreaByCityId(cid);
        return  this.convertEntityToVo(provinces);
    }


    private List<SysRegionVO> convertEntityToVo(List<SysRegion> regions){
        return regions.stream()
                .map(p -> BeanUtil.copyProperties(p, SysRegionVO.class))
                .collect(Collectors.toList());

    }

}



